﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.OleDb;

namespace GreenBuildings
{
    public partial class details : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false) //if arrived from different page
            {
                if (Request.Params["ID"] == "")
                {
                    Response.Redirect("list.aspx");
                }

                else
                {
                    //showing comment in gridview2 through code by populating from datasource
                    string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Buildings.accdb";
                    string cmdstr = "select * from Comments where Buildings_fk= @f1";
                    OleDbConnection con = new OleDbConnection(constr);
                    OleDbCommand cmd = new OleDbCommand(cmdstr, con);
                    con.Open();
                    cmd.Parameters.AddWithValue("@f1", Request.Params["ID"]);
                    //  OleDbDataReader rdr = null;
                    OleDbDataReader rdr = cmd.ExecuteReader();
                    GridView2.DataSource = rdr;
                    GridView2.DataBind();
                    con.Close();
                }
            }
        }

        protected void btn_AddComment_Click(object sender, EventArgs e)
        {
            string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Buildings.accdb";
            string cmdstr = "insert into Comments(Author, Comment, Title, Buildings_fk) values(@f1,@f2,@f3,@f4)";

            OleDbConnection con = new OleDbConnection(constr);
            OleDbCommand cmd = new OleDbCommand(cmdstr, con);
            con.Open();
            cmd.Parameters.AddWithValue("@f1", tb_Author.Text);
            cmd.Parameters.AddWithValue("@f2", tb_Comment.Text);
            cmd.Parameters.AddWithValue("@f3", tb_ComTitle.Text);
            cmd.Parameters.AddWithValue("@f4", Request.Params["ID"]);
            cmd.ExecuteNonQuery();

            //Refresh or reload grid2 after comment
            string reloadComment = "select * from Comments where Buildings_fk= @f1";
            OleDbCommand reloadCommentCmd = new OleDbCommand(reloadComment, con);
            reloadCommentCmd.Parameters.AddWithValue("@f1", Request.Params["ID"]);
            OleDbDataReader rdr = reloadCommentCmd.ExecuteReader();
            GridView2.DataSource = rdr;
            GridView2.DataBind();
            tb_Author.Text = "";
            tb_Comment.Text = "";
            tb_ComTitle.Text = "";
            con.Close();
            //Response.Redirect("commentConfirmation.aspx");

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Buildings.accdb";
            string cmdstr = "insert into BuildingTags(Building_fk, Tag_fk) values(@f1,@f2)";
            OleDbConnection con = new OleDbConnection(constr);
            OleDbCommand cmd = new OleDbCommand(cmdstr, con);
            con.Open();

            foreach (ListItem cb  in CheckBoxList1.Items)
            {
                if (cb.Selected)
                {
                    //join tag and buildings table
                                                          
                    cmd.Parameters.AddWithValue("@f1", Request.Params["ID"]);
                    cmd.Parameters.AddWithValue("@f2", cb.Value);
                    cmd.ExecuteNonQuery();
                }
            }
         //   con.Open();
       //     string newTags = TextBox1.Text.ToString ();
            
       //     char[] seperator = new char[] { ',' };
         //   string[] newTag = newTags.Split(seperator);
         //   string newTag = newTags.Split(',');
        //     foreach (newTag  in newTags)
         //    {}

            char[] delimiterChar = { ' ', ',' , '.', ':', ';', '\t' };
            string tagWords = TextBox1.Text;

            string[] newTag = tagWords.Split(delimiterChar);
            foreach (string s in newTag)
            {
               // add new tag to tag table
                string newTagSql = "insert into Tags(Tag) values(@f1)";
                
                OleDbCommand tagCmd = new OleDbCommand(newTagSql, con);
         //       con.Open();
             //   tagCmd.Parameters.AddWithValue("@f1", newTag);
               tagCmd.Parameters.AddWithValue("@f1", s);
                tagCmd.ExecuteNonQuery();

                //find id of the tag
                OleDbCommand getLastIdCmd = new OleDbCommand("select top 1 ID from Tags order by ID desc", con);
                OleDbDataReader dr = getLastIdCmd.ExecuteReader();
                dr.Read();
                int lastId = Convert.ToInt32( dr.GetValue(0));
          //      con.Close();

                //link last id to building id
                OleDbCommand newCmd = new OleDbCommand(cmdstr, con);
           //       con.Open();
                                                                          
                    newCmd.Parameters.AddWithValue("@f1", Request.Params["ID"]);
                    newCmd.Parameters.AddWithValue("@f2",lastId);
                    newCmd.ExecuteNonQuery();
           //         con.Close();
                }
            }



        }
    }
